import 'package:chathouse/util/base_utils.dart';
import 'package:flutter/material.dart';

import '../widgets/base_appbar.dart';

class ChatImageBrowser extends StatefulWidget {
  final List<String> imageUrls;
  Function onClosePressed;
  int? currentIndex;

  ChatImageBrowser(this.onClosePressed,
      {required this.imageUrls, this.currentIndex});

  @override
  _NetworkImageBrowserState createState() => _NetworkImageBrowserState();
}

class _NetworkImageBrowserState extends State<ChatImageBrowser> {
  int _currentIndex = 0;
  PageController? _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        widget.onClosePressed;
        return false;
      },
      child: Scaffold(
        backgroundColor: Colors.black,
        appBar: getAppBar(title: "圖片瀏覽"),
        body: PageView.builder(
          controller: _pageController,
          itemCount: widget.imageUrls.length,
          // onPageChanged: (index) {
          //   setState(() {
          //     _currentIndex = index;
          //   });
          // },
          itemBuilder: (BuildContext context, int index) {
            return Image.network(widget.imageUrls[index], fit: BoxFit.fitWidth).setOnClickListener((){
              widget.onClosePressed;
            });
          },
        ),
      ),
    );
  }

  List<Widget> _buildIndicator() {
    List<Widget> indicators = [];
    for (int i = 0; i < widget.imageUrls.length; i++) {
      indicators.add(Container(
        margin: EdgeInsets.symmetric(horizontal: 4),
        width: 10,
        height: 10,
        decoration: BoxDecoration(
          color: _currentIndex == i ? Colors.white : Colors.grey,
          borderRadius: BorderRadius.circular(5),
        ),
      ));
    }
    return indicators;
  }
}
